//
//  HomeViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/8/27.
//  Copyright © 2016年 王道林. All rights reserved.
//
#import "DLMacro.h"
#import "DLWNetworkingParser.h"
#import "HomeViewController.h"
#import "BaseCollectionView.h"
#import "Home_CollectionViewCellFunction.h"
#import "Home_CollectionViewCellNews.h"
#import "Home_CollectionReusableViewCycle.h"
#import "Home_CycleModel.h"
#import "Home_NewsModel.h"
#import "Home_NewsViewControllerWeb.h"
#import "RetrieveViewController.h"
#import "Hot_ViewController.h"
#import "Cloud_ViewController.h"
#import "Insure_ViewController.h"
#import "Shop_ViewController.h"
#import "Action_ViewController.h"
#import "ProductEffect_ViewController.h"
#import "LogonViewController.h"

@interface HomeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrCycle;
@property (nonatomic, strong) NSMutableArray *arrNews;
@property (nonatomic, strong) NSArray *arrName;
@property (nonatomic, strong) NSArray *arrImage;


@end

@implementation HomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self handleData];
    [self createSubview];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}


#pragma mark - 获取数据
- (void)handleData
{
    // 中间8个的数据
    _arrName = @[@"热点资讯",@"云检测",@"蛋鸡保险",@"管家商城",@"蛋价地图",@"蛋价曲线",@"生产效益",@"活动中心"];
    _arrImage = @[@"zi xun",@"jian ce",@"bao xian",@"jfen",@"di tu",@"qu xian",@"xiaoyi",@"huodong"];
    
    _arrCycle = [NSMutableArray array];
    _arrNews = [NSMutableArray array];
    [DLWNetworkingParser POSTWithURL:@"http://www.danjiguanjia.com/api/News/getRedlineTop" par:nil success:^(id responseObject) {
        // 轮播图数据
        for (NSDictionary *dic in responseObject[@"ticket"][@"banner"]) {
            Home_CycleModel *model = [Home_CycleModel modelWithDic:dic];
            [_arrCycle addObject:model];
        }
        // 下面新闻数据
        for (NSDictionary *dic in responseObject[@"ticket"][@"RedlineTop"]) {
            Home_NewsModel *model = [Home_NewsModel modelWithDic:dic];
            [_arrNews addObject:model];
        }

        [_collectionView reloadData];
    } filed:^(NSError *error) {

    }];
}

#pragma mark - 创建子控件
- (void)createSubview
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:self action:nil];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(VIEW_WIDTH, 100);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 0, 0, 0);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    _collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, self.view.frame.size.height - 49 - 64) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    // 注册第一cell
    [_collectionView registerClass:[Home_CollectionViewCellFunction class] forCellWithReuseIdentifier:@"reuseOne"];
    // 注册第二个cell
    [_collectionView registerClass:[Home_CollectionViewCellNews class] forCellWithReuseIdentifier:@"reuseTwo"];
    // 注册头的view
    [_collectionView registerClass:[Home_CollectionReusableViewCycle class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuseHead"];
}

#pragma mark - collection 的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 8;
    }else
    {
    return _arrNews.count;
    }
}

// 返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        Home_CollectionViewCellFunction *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseOne" forIndexPath:indexPath];
        cell.label.text = _arrName[indexPath.row];
        cell.iamgeView.image = [UIImage imageNamed:_arrImage[indexPath.row]];
        return cell;
    } else
    {
        Home_CollectionViewCellNews *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseTwo" forIndexPath:indexPath];
//        cell.backgroundColor = COLORAUTO;
        Home_NewsModel *model = _arrNews[indexPath.row];
        cell.model = model;
        return cell;
    }
}
// 返回头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Home_CollectionReusableViewCycle *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuseHead" forIndexPath:indexPath];
        view.marr = self.arrCycle;
        // 实现block,轮播图 推出下一界面
        view.blockModel = ^(Home_CycleModel *model)
        {
            if ([model.url isEqualToString:@"忘记密码"]) {
                RetrieveViewController *VC = [[RetrieveViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            } else
            {
                Home_NewsViewControllerWeb *VCWeb = [[Home_NewsViewControllerWeb alloc] init];
                // 用于判断
                VCWeb.name = @"cycle";
                VCWeb.url = model.url;
                VCWeb.navigationTitle = @"资讯详情";
                [self.navigationController pushViewController:VCWeb animated:YES];
            }
        };
        return view;
    }
    return nil;
}


#pragma mark - flowYayout的协议
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(VIEW_WIDTH / 4, 120);
    } else
    {
        return CGSizeMake(VIEW_WIDTH, 80);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(VIEW_WIDTH, 200);
    }
    return CGSizeMake(0, 0);
}

#pragma mark - collectionView的点击方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        Home_NewsViewControllerWeb *VCWeb = [[Home_NewsViewControllerWeb alloc] init];
        VCWeb.name = @"news";
        VCWeb.newsId = [_arrNews[indexPath.row] cms_id];
        VCWeb.navigationTitle = @"资讯详情";
        [self.navigationController pushViewController:VCWeb animated:YES];
    } else if (indexPath.section == 0)
    {
        if (indexPath.row == 0) {
            Hot_ViewController *hotVC = [[Hot_ViewController alloc] init];
            [self.navigationController pushViewController:hotVC animated:YES];
        } else if (indexPath.row == 1)
        {
            Cloud_ViewController *cloudVC = [[Cloud_ViewController alloc] init];
            [self.navigationController pushViewController:cloudVC animated:YES];
        }
        else if (indexPath.row == 2)
        {
            Insure_ViewController *InsureVC = [[Insure_ViewController alloc] init];
            [self.navigationController pushViewController:InsureVC animated:YES];
        }
        else if (indexPath.row == 3)
        {
            Shop_ViewController *shopVC = [[Shop_ViewController alloc] init];
            [self.navigationController pushViewController:shopVC animated:YES];
        }
        else if (indexPath.row == 4)
        {
            Home_NewsViewControllerWeb *VCWeb = [[Home_NewsViewControllerWeb alloc] init];
            VCWeb.name = @"map";
            VCWeb.navigationTitle = @"蛋价地图";
            [self.navigationController pushViewController:VCWeb animated:YES];
        }
        else if (indexPath.row == 5)
        {
            Home_NewsViewControllerWeb *VCWeb = [[Home_NewsViewControllerWeb alloc] init];
            VCWeb.name = @"price";
            VCWeb.navigationTitle = @"蛋价曲线";
            [self.navigationController pushViewController:VCWeb animated:YES];
        } else if (indexPath.row == 6)
        {
            ProductEffect_ViewController *productVC = [[ProductEffect_ViewController alloc] init];
            [self.navigationController pushViewController:productVC animated:YES];
        }
        else if (indexPath.row == 7)
        {
            
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            NSString *ticket = [userDefault valueForKey:@"ticket"];
            NSString *uid = [userDefault valueForKey:@"uid"];
            if (ticket == nil && uid == nil)
            {
                LogonViewController *logonVC = [[LogonViewController alloc] init];
                [self.navigationController pushViewController:logonVC animated:NO];
            } else
            {
                Action_ViewController *actionVC = [[Action_ViewController alloc] init];
                [self.navigationController pushViewController:actionVC animated:YES];
            }
        }
        
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
